package com.fenghongzhang.day006;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class VpAdapter extends PagerAdapter {

    private Context context;
    private List<String> list;

    public VpAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }
    /**
     * 多少页
     * @return
     */
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        View inflate = LayoutInflater.from(context).inflate(R.layout.vp_layout, null);

        TextView textView = inflate.findViewById(R.id.vp_tv);
        textView.setText(list.get(position));

        //要把创建的view添加到container中
        container.addView(inflate);

        return inflate;
    }
}
